<?php


namespace app\common\service;


use app\model\SysConfig;
use think\facade\Db;

class ConfigService
{

    static function getConfig($key)
    {
        $data = SysConfig::where("key", '=', $key)
            ->find();
        return json_decode($data['value']??"[]", true)??[];
    }

    static function getData($key)
    {
        $data = SysConfig::where("key", '=', $key)
            ->find();
        $data['value'] = json_decode($data['value'], true)??[];
        return $data;
    }

    static function setConfig($key,$value)
    {
        $ex = SysConfig::where("key", '=', $key)
            ->count()>0;
        if ($ex){
            SysConfig::where("key", '=', $key)
                ->update([
                    'value'=>json_encode($value),
                ]);
        }else{
            SysConfig::where("key", '=', $key)
                ->insert([
                    'key'=>$key,
                    'value'=>json_encode($value),
                ]);
        }

    }
}